﻿CREATE Function dbo.f_AverageStatus
(@LoanID int)
RETURNS numeric(5,2) WITH SCHEMABINDING BEGIN

DECLARE @Ret numeric(5,2)

	SELECT @Ret = SUM(HighCredit*Status*MR)/CONVERT(float,NULLIF(SUM(HighCredit*MR),0))
	FROM dbo.v_TradeReferences
	WHERE LoanID = @LoanID
				AND Status > 0
				AND AccType IN ('I','R')
--				 AND AccountState = 1
				AND DATEDIFF(mm,LastDate,DatePulled) <= 12

RETURN  @Ret
END






